// pages/search/search.js
import { wxRequest } from "../../utils/ajax";
Page({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    current: 0,
    pageStart: 1,
    pageTotal: 5,
    paging: true,
    keyword :null,
  },

  /**
   * 组件的方法列表
   */
  methods: {

  },
  getArticles: function (val, pageStart, pageTotal) {
    let that = this;
    if (val != undefined && val != '') {
      wxRequest('/aArticle/search', {
        keyword: val, pageStart: pageStart, pageTotal: pageTotal
      }, function (data) {
        let arr = that.data.list;
        let res = data.data.data;
        var tempList = null;
        if (pageStart == 1) {
          arr = [];
          that.setData({
            pageStart: pageStart
          });
        }
        if (res == null) {
          that.setData({
            paging: false,
            list: null,
            pageStart: 1
          });
        } else {
          if (res.length < pageTotal) {
            that.setData({
              paging: false,
              list: arr.concat(res),
            });
          } else {
            that.setData({
              paging: true,
              pageStart: pageStart + 1,
              list: arr.concat(res),
            });
          }
        }
      });
    }
  },
  search(e){
    let val = e.detail.value.keyword
    this.data.keyword = val;
    this.getArticles(val, 1, this.data.pageTotal);
  },
  onReachBottom: function () {
    let that = this;
    if (that.data.paging) {
      that.getArticles(this.data.keyword , that.data.pageStart, that.data.pageTotal);
    }
  },
  bindArticleDetail: function (e) {
    let artId = e.currentTarget.dataset.id;
    console.log(artId);
    if (artId != "" && artId != undefined) {
      wx.navigateTo({
        url: '/pages/detail/detail?artId=' + artId
      })
    }
  },
})
